package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDish(DishDTO dishDTO) {

//        添加dish  返回主键dishid
        Dish dish = Dish.builder().build();

        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.insert(dish);

        Long dishId = dish.getId();
        log.info("菜品id：{}", dishId);


//        添加口味表 dish floaver 表
//获取口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
//        给口味表中的口味信息，设置菜品表‘

        for (DishFlavor flavor : flavors) {

            flavor.setDishId(dishId);
        }
//        请问：口味绑定菜品了吗？
//        批量插入口味

        dishFlavorMapper.insertBatch(flavors);


    }

    @Override
    public PageResult pageByCondition(DishPageQueryDTO dqd) {
//        开启分页插件

        PageHelper.startPage(dqd.getPage(), dqd.getPageSize());

        if (dqd.getName() != null) {
            dqd.setName(dqd.getName().trim());
        }

        Page<DishVO> page = dishMapper.pageByCondition(dqd.getName(), dqd.getCategoryId(), dqd.getStatus());


        PageResult pageResult = new PageResult();

        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());


        return pageResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<Long> ids) {

//        删除菜品
//        66,68
//根据菜品id查询菜品的状态为启用的个数
        Integer statuscount = dishMapper.statusByIds(ids);
//        什么时候不能删？
//            启用状态不能删
        if (statuscount > 0) {
            //          抛异常，不能删除，
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);

        }
        // 根据菜品ids查询被套餐引用的个数
        //     被套餐引用不能删
        Integer setmealcount = setmealDishMapper.getCountByDishIds(ids);

        if (setmealcount > 0) {
            //          抛异常，不能删除，
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);

        }
        // 根据菜品ids批量删除菜品
//            删除菜品
        dishMapper.deleteByIds(ids);

//        删除口味

        //根据菜品ids批量删除口味信息
        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    public DishVO getDishWithFlavorByDishId(Long id) {

//        创建VO
        DishVO dishVO = DishVO.builder().build();

//        设置菜品的基本信息
        Dish dish=  dishMapper.getById(id);
        BeanUtils.copyProperties(dish,dishVO);
//        设置菜品的口味信息
        List<DishFlavor> dishFlavors= dishFlavorMapper.getFlavorsByDishId(id);
        dishVO.setFlavors(dishFlavors);

        return dishVO;
    }

    @Override
    public void updateDishWithFlavor(DishDTO dishDTO) {



//        修改基本表菜品表
        Dish dish = Dish.builder().build();
        BeanUtils.copyProperties(dishDTO,dish);

        Long dishId = dish.getId();

        dishMapper.update(dish);


//        删除口味表

        ArrayList<Long> ids = new ArrayList<>();
        ids.add(dishId);
        dishFlavorMapper.deleteByDishIds(ids);


//     批量插入口味
//        添加口味表 dish floaver 表
//获取口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
//        给口味表中的口味信息，设置菜品表‘

        if (flavors!=null && flavors.size()>0){

        for (DishFlavor flavor : flavors) {

            flavor.setDishId(dishId);
        }
//        请问：口味绑定菜品了吗？
//        批量插入口味

        dishFlavorMapper.insertBatch(flavors);

        }

    }

    /**
     *根据Id查询菜品
     */
    @Override
    public List<DishVO> getByIdDish(Dish dish) {
        List<Dish> dishList = dishMapper.getByCategoryId(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlavorsByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;

    }
}
